<template>
	<view class="header-bg">
		<!-- #ifdef MP-WEIXIN || MP-ALIPAY  -->
		<u-navbar title="我的会员码" bgColor="rgba(255,255,255,0)" leftIconSize="0rpx" :autoBack="true"></u-navbar>
		<!-- #endif -->
		<view class="body">
			<view class="member">
				<view>
					<u-avatar :src="headImgUrl" size="50"></u-avatar>
				</view>
				<view class="upper-right">
					<view class="widhtes">
						<view class="name">
							<text>{{nickName}}</text>
							<text class="u-font-24" v-if="memberId">（{{this.$t('qr-code.members')}}ID：{{memberId}}）</text>
						</view>
						<view class="phone" v-if="mobile">{{mobile}}</view>
					</view>
					<!-- <u-button shape="circle" :customStyle="nextStepBtnCustomStyle" text="立即充值"></u-button> -->
				</view>
			</view>
			<view class="qr-code">
				<view class="frame" @click="getCheckCodeQr">
					<image class="image_406" :src="memberCheckCodeQr"></image>
				</view>
				<view class="produce">{{this.$t('qr-code.show')}}</view>
				<view class="renew">{{$t('qr-code.thisQrCode')}} {{validity}}{{$t('qr-code.effectiveBefore')}}</view>
			</view>
		</view>
    <u-toast ref="uToast"></u-toast>
    <tabbar v-if="isEs" ref="tab"></tabbar>
	</view>
</template>

<script>
import tabbar from "@/components/tabbar/tabbar.vue"
export default {
  components:{
    tabbar
  },
		data() {
			return {
				nextStepBtnCustomStyle: {
					width: '174rpx',
					height: '72rpx',
					background: 'rgb(126, 125, 243);',
					borderRadius: '200px',
					color: '#fff',
				},
				memberId: '',
				validity: '',
				memberCheckCodeQr: '',
				headImgUrl: '',
				mobile: '',
				nickName: '',
        //是否是西班牙语
        isEs:this.$i18n.locale=='es',
			}
		},
  onShow(){
    this.isEs=this.$i18n.locale=='es'
    // #ifdef  H5
    if (this.isEs){
        this.$nextTick(() => {
          this.$refs.tab.show=getApp().globalData.tab.show
          this.$refs.tab.name=getApp().globalData.tab.name
        })
      uni.hideTabBar()
    }
    // #endif
  },
		onLoad() {
      //隐藏tabbar
			this.findMemberInfo()
		},
		methods:   {
			findMemberInfo() {
				uni.showLoading({
					title: '加载中...'
				})
				this.$api('member.info', null).then(res => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					if (res.status == 0) {
						this.memberId = res.data.id;
						this.headImgUrl = res.data.headImgUrl
						this.mobile = res.data.mobile
						this.nickName = res.data.nickName
						
						this.getCheckCodeQr()
					} else {
						getApp().err("会员数据加载不成功：" + res.msg);
					}
			
				}).catch(err => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				})
			},
			
			//生成会员码
			getCheckCodeQr() {
				this.$api('member.getCheckCodeQr',{'language':this.$i18n.locale}).then(res => {
					if (res.status == 0) {
            if (res.data.memberId){
                this.memberCheckCodeQr = this.$mWxQrCode.createQrCodeImg(res.data.tempId, {
                  size: parseInt(165) //二维码大小
                })
                this.memberId = res.data.memberId;
                this.validity = res.data.validity;
            }else{
              if (!this.isEs){
                this.$refs.uToast.show({
                  message:res.data+'秒内不可刷新!',
                })
              }
            }
						uni.hideLoading()
					} else {
						this.$helpers.toast('请求数据失败:' + res.msg);
					}
				}).catch(err => {
					uni.hideLoading()
				})
			},
		}
	}
</script>

<style lang="scss">
	.header-bg {
		width: 100%;
		height: 601rpx;
		background: radial-gradient(92.46% 74.03% at 69% 35%, rgb(219, 222, 255), rgb(247, 247, 247) 100%);
		border: 1rpx transparent solid;
		.body {
			background: rgb(255, 255, 255);
			border-radius: 20rpx;
			margin: 268rpx 25rpx;
			padding: 40rpx 36rpx;
			display: block;

			.member {
				display: flex;

				.upper-right {
					display: flex;
					flex: 1;
					align-items: center;
					margin-left: 24rpx;

					.widhtes {
						flex: 1;
					}

					.name {
						color: rgb(34, 34, 34);
						font-size: $f-lg;
					}

					.phone {
						color: rgb(102, 102, 102);
						font-size: $f-base;
						margin-top: 9rpx;
					}
				}
			}

			.qr-code {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin: 40rpx 0 100rpx 0;

				.frame {
					width: 430rpx;
					height: 430rpx;
					box-sizing: border-box;
					//border: 1rpx solid rgba(0, 0, 0, 0.2);
					border-radius: 10rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
				}

				.produce {
					color: rgb(34, 34, 34);
					font-size: $f-base;
					margin-top: 30rpx;
				}

				.renew {
					color: rgb(153, 153, 153);
					font-size: $f-base;
					margin-top: 10rpx;
				}
			}

			.image_406 {
				width: 406rpx;
				height: 406rpx;
			}
		}
	}
</style>
